package code;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import newtank.data.ToServerDataPackage;
import server.GpcRequest;
import server.netty.GpcDataPackage;

/**
 * @author lijiahao
 */
public class GpcDataServerEncoder extends MessageToByteEncoder<ToServerDataPackage> {


	@Override
	protected void encode(ChannelHandlerContext context, ToServerDataPackage msg, ByteBuf out) throws Exception {
		GpcDataPackage gpcDataPackage = msg.toData();
		if(gpcDataPackage.getCode() == 0) {
			gpcDataPackage.setRouteRequest(new GpcRequest());
		}
		ObjectMapper mapper = new ObjectMapper();
		byte[] data = mapper.writeValueAsBytes(gpcDataPackage);
		out.writeInt(data.length);
		out.writeBytes(data);
	}

}
